#pragma once
#include "err.hpp"
#include <iostream>
#include <sys/types.h>
#include <pthread.h>
#include <sys/socket.h>
#include <cstring>
#include <cstdlib>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <functional>
#include "RingQueue.hpp"
#include <unordered_map>
#include "lockGuard.hpp"
#include "Thread.hpp"

namespace ns_server
{

    const static uint16_t default_port = 8080;
    using func_t = std::function<std::string(std::string)>;
    class UdpServer
    {
    public:
        UdpServer(uint16_t port = default_port) : port_(port)
        {
            std::cout << "server addr: "
                      << " : " << port_ << std::endl;
            pthread_mutex_init(&lock, nullptr);
            c = new Thread(1, std::bind(&UdpServer::Recv, this));
            p = new Thread(1, std::bind(&UdpServer::Broadcast, this));
        }
        ~UdpServer()
        {
            pthread_mutex_destroy(&lock);
            c->join();
            p->join();
            delete c;
            delete p;
        }
        void Start()
        {
            // 1.创建套接字接口（打开网络文件）
            sock_ = socket(AF_INET, SOCK_DGRAM, 0);
            if (sock_ < 0)
            {
                std::cerr << "create socket error:" << strerror(errno) << std::endl;
                exit(SOCKET_ERR);
            }
            std::cout << "create socket succes:" << sock_ << std::endl;
            // 2.指明ip地址和端口号
            // 2.1定义sockaddr_in结构体->初始化字段->bind
            struct sockaddr_in local;
            bzero(&local, sizeof(local)); // 清空
            // 2.1.1 16位地址类型
            local.sin_family = AF_INET;
            // 2.1.2 端口号
            local.sin_port = htons(port_); // 端口要发到网络当中 --主机序列转网络序列
            // 2.1.3 ip
            // 字符串风格的ip地址转化成4字节的整数 “1.1.1.1”-->uint32_t
            //     主机序列->网络序列
            //  使用 ：in_addr_t inet_addr(const char* cp)
            // 云服务器不能指明ip
            //  local.sin_addr.s_addr=inet_addr(ip_.c_str());
            local.sin_addr.s_addr = INADDR_ANY; // bind本主机ip
            int n = bind(sock_, (struct sockaddr *)&local, sizeof(local));
            if (n < 0)
            {
                std::cerr << "bind socket error:" << strerror(errno) << std::endl;
                exit(BIND_ERR);
            }
            p->run();
            c->run();
        }
        void addUser(const std::string &name, const struct sockaddr_in &peer)
        {
            LockGuard lg(&lock);
            onlineuser[name] = peer;
        }
        void Recv()
        {
            char buffer[1024];
            while (true)
            {
                // 收
                struct sockaddr_in peer;
                socklen_t len = sizeof(peer); // 写清楚传入缓冲区大小
                int n = recvfrom(sock_, buffer, sizeof(buffer) - 1, 0, (struct sockaddr *)&peer, &len);
                if (n > 0)
                {
                    buffer[n] = '\0';
                }
                else
                    continue;
                // 提取client信息
                std::string clientip = inet_ntoa(peer.sin_addr);
                uint16_t clientport = ntohs(peer.sin_port);
                std::cout << clientip << "-" << clientport << "get message# " << buffer << std::endl;
                // 构建一个用户,并检查
                std::string name = clientip;
                name += "-";
                name += std::to_string(clientport);
                // 如果用户不存在则adduser
                addUser(name, peer);
                // push到环形队列
                std::string message=name+">>"+buffer;
                rq.push(message);
            }
        }
        void Broadcast()
        {
            while (true)
            {
                std::string sendstring;
                rq.pop(&sendstring);
                LockGuard lg(&lock);

                for (auto user : onlineuser)
                {
                    std::cout << "broadcast message to" << user.first << ":" << sendstring << std::endl;
                    sendto(sock_, sendstring.c_str(), sendstring.size(), 0, (struct sockaddr *)&(user.second), sizeof(user.second));
                }
            }
        }

    private:
        uint16_t port_;
        int sock_;
        // func_t service_;
        // std::string ip_;//字符串风格的点分十进制
        std::unordered_map<std::string, struct sockaddr_in> onlineuser;
        RingQueue<std::string> rq;
        pthread_mutex_t lock;
        Thread *c;
        Thread *p;
    };
}